/**
 * -------------------------------------------------------------
 * 页面渲染中间件-查询表格
 * -------------------------------------------------------------
 * author: niewei / 2022-08-12
 * -----------------------------------------------------------
 */
 import createState from 'mars-pro/lib/pro/createState'
 const titleCase = (prefix,str) => {
    let newStr = str.slice(0,1).toUpperCase() + str.slice(1).toLowerCase();
    return prefix + newStr;
}

// 生成表格操作插槽
const OPERATE_COLUMN = (data) => {
    const OPERATE_BTN = (data.operateConfigList || []).filter(item => {
        return item.positionArea === 'OPERATE_COLUMN'
    })
    return OPERATE_BTN.map((item) => {
        const propsData =(item) => {
            let propsData = {}
            if(item.displayType === 'BUTTON' && item.operateIcon){
                propsData.icon = item.operateIcon
            }
            if(item.displayType === 'ICON'){
                propsData.icon = item.operateIcon
                propsData.type = 'text'
            }
            return propsData
        }
        return {
            tag:'pro-action',
            props:propsData(item),
            attrs:propsData(item),
            on:{},
            slots: {
                default:[item.displayType === 'ICON' ? '' : item.operateName]
            }
        }
    })
}

const dataTypeAttrs = (data) => {
    let quotaDefineList = data.quotaDefineList.filter(item => {
        return item.searchAttrs
    })
    return quotaDefineList.map(item => {
        return {
            tag:'pro-input',
            props:{
                key:item.quotaCode,
                uiType:'text',
                'v-model': `form.search.${item.quotaCode}`,
                prop:`${item.quotaCode}`,
                label:item.quotaName
            },
            attrs:{
                key:item.quotaCode,
                uiType:'text',
                'v-model': `form.search.${item.quotaCode}`,
                prop:`${item.quotaCode}`,
                label:item.quotaName
            }
        }
    })
}
// 生成表格上方的操作按钮
const TOOLBAR_RENDER = (data,pos) => {
    if(pos !== 'left' && pos !== 'right'){
        return []
    }
    let TOOLBARNAME = ''
    if(pos === 'left'){
        TOOLBARNAME = 'LEFT_TOOLBAR'
    }
    if(pos === 'right'){
        TOOLBARNAME = 'RIGHT_TOOLBAR'
    }
    const TOOLBAR_BTN = (data.operateConfigList || []).filter(item => {
        return item.positionArea === TOOLBARNAME
    })
    const propsData =(item) => {
        let propsData = {}
        if(item.displayType === 'BUTTON' && item.operateIcon){
            propsData.icon = item.operateIcon
        }
        if(item.displayType === 'ICON'){
            propsData.icon = item.operateIcon
            propsData.type = 'text'
        }
        return propsData
    }
    return TOOLBAR_BTN.map((item) => {
        return {
            tag:'pro-action',
            props:propsData(item),
            attrs:propsData(item),
            on:{},
            slots: {
                default:[item.displayType === 'ICON' ? '' : item.operateName]
            }
        }
    })
}
//生成表单操作按钮
const SEARCH_AREA = (data) => {
    const SEARCH_BTN = (data.operateConfigList || []).filter(item => {
        return item.positionArea === 'SEARCH_AREA'
    })
    
    let _SEARCH_BTN = SEARCH_BTN.map((item) => {
        const propsData =(item) => {
            let propsData = {}
            if(item.displayType === 'BUTTON' && item.operateIcon){
                propsData.icon = item.operateIcon
            }
            if(item.displayType === 'ICON'){
                propsData.icon = item.operateIcon
                propsData.type = 'text'
            }
            return propsData
        }
        // doSearch, doReset, handleMore,showMore
        console.log(titleCase('do',item.operateCode))
        return {
            tag:'pro-action',
            props:propsData(item),
            attrs:propsData(item),
            on:{
                "@handler":'handlers.' + titleCase('do',item.operateCode)
            },
            slots: {
                default:[item.displayType === 'ICON' ? '' : item.operateName]
            }
        }
    })
    _SEARCH_BTN.push(
        {
            tag:'pro-action',
            "class":{
                ":handeToggle":true
            },
            on:{
                "@handler":'handlers.handleMore'
            },
            slots: {
                default:[
                    {
                        tag:'i',
                        "class":{
                            ':el-icon-caret-top':'handlers.showMore === true',
                            ':el-icon-caret-bottom':'handlers.showMore === false'
                        }
                    }
                    
                ]
            }
        }
    )
    return _SEARCH_BTN
}
// 生成页面级data数据
const renderData = (data) => {
    let columns = []
    let vDatas = {
        ":columns":[],
        ":form":  createState({":search": {}}),
        ":selectRows":[],
        ":baseSearchKeys": null
    };
    
    (data.quotaDefineList || []).forEach(item => {
        columns.push( {prop: item.quotaCode,label: item.quotaName })
    });
    const OPERATE_BTN = (data.operateConfigList || []).filter(item => {
        return item.positionArea === 'OPERATE_COLUMN'
    })
    if(OPERATE_BTN.length > 0){
        columns.push({
            prop:"action",
            label:'操作'
        })
    }
    vDatas[":columns"] = columns
    return vDatas
}
// 生成页面数据源定义
const renderDataSourcce = (data) => {
    // data.respDataField
    // data.respTotalField
    // data.apiPrefix 接口地址
    //[  
    //        {
    //         name: 'dataSourcce',
    //         url: '/api/use/list',
    //         method: 'get',
    //         params: {},
    //         multiple: false
    //        }
    //      ]
    let vDatas = {
        name:'tableDatas',
        url:data.apiPrefix,
        params:{}
    };
    console.log(vDatas)
    return [vDatas]
}
// 生成列表查询模板
const renderTemplate = (data) => {
    let vDatas = {
        tag:'div',
        "class":{
            ':module-main':true
        },
        slots: {
            default:[
                {
                    tag:'el-form',
                    props:{
                        ':model':'form.search',
                        ref:'elform',
                        labelWidth:'120px'
                    },
                    attrs:{
                        ':model':'form.search',
                        ref:'elform',
                        labelWidth:'120px'
                    },
                    slots: {
                        default:[
                            {
                                tag:'pro-layout-content-search',
                                props:{
                                    ':baseCount':3,
                                    ':columnCount':3
                                },
                                attrs:{
                                    ':baseCount':3,
                                    ':columnCount':3
                                },
                                on:{
                                    "@search":'handleSearch',
                                    "@reset":'handleResetFields'
                                },
                                slots: {
                                    default:dataTypeAttrs(data)
                                },
                                scopedSlots:{
                                    'action(handlers)':[
                                        {
                                            tag:'div',
                                            "class":{
                                                ':actionClass':true
                                            },
                                            slots:{
                                                default:SEARCH_AREA(data)
                                            }
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                },
                {
                    tag:'div',
                    styles:[
                        {
                            'margin-top':'15px'
                        }
                    ],
                    slots:{
                        default:[
                            {
                                tag:'div',
                                "class":{
                                    ':table-operator':true
                                },
                                slots:{
                                    default:[
                                        {
                                            tag:'div',
                                            "class":{
                                                ':table-operator-left':true
                                            },
                                            slots:{
                                                default:TOOLBAR_RENDER(data,'left')
                                            }
                                        },
                                        {
                                            tag:'div',
                                            "class":{
                                                ':table-operator-right':true
                                            },
                                            slots:{
                                                default:TOOLBAR_RENDER(data,'right')
                                            }
                                        }
                                    ]
                                },
                            },
                            {
                                tag:'pro-data-table',
                                props:{
                                    ref:'prodatatable',
                                    ':dataSource':'tableDatas',
                                    ':columns':'columns',
                                    rowKey:'id'
                                },
                                attrs:{
                                    ref:'prodatatable',
                                    ':dataSource':'tableDatas',
                                    ':columns':'columns',
                                    rowKey:'id'
                                },
                                scopedSlots: {
                                    'actionContent':OPERATE_COLUMN(data)
                                }
                            }
                        ]
                    }
                }
            ]
        }
    }
    return vDatas
}
// 生成页面级方法集合
const renderMethods = (data) => {
    return {
        "handleSearch(baseProps)":`this.baseSearchKeys = baseProps
        const pagination = this.tableDatas.pagination || {}
        const {currentPageField,pageSizeField} = this.getConfigProvide().getApiParam();// this.PRO_CONFIG_PROVIDE_CONTEXT.config.api
        this.tableDatas.loadDatas({
            ...this.form.search,
            [pageSizeField]: pagination.pageSize || 10,
            [currentPageField]: 1
        })`,
        "handleResetFields":`this.$refs.elform.resetFields()
        this.handleSearch(this.baseSearchKeys)`,
        "doHandle(row, store, column)":`console.log(row, store, column)`,
        "handleRefresh":`this.tableDatas.loadDatas()`,
        "handleUpload":`console.log('handleUpload')`
    }
}
// 生成页面集合
export const renderPage = (data) => {
    data = {
        "id": "220809105940012761",
        "classifyId": null,
        "mainObject": "CommDataFunctionConfig",
        "listName": "数据功能生效",
        "listCode": "commDataFunctionConfig",
        "apiPrefix": "reusable/commDataFunctionConfig",
        "permissionPrefix": "commDataFunctionConfig",
        "listDisplayType": 1,
        "leftFixedColumns": 0,
        "rightFixedColumns": 1,
        "paging": 1,
        "treeParentField": null,
        "respDataField": "result.records",
        "respTotalField": "result.total",
        "enabled": 1,
        "remark": null,
        "treeType": null,
        "nodeText": null,
        "nodeCode": null,
        "objectTypeText": null,
        "objectTypCode": null,
        "createBy": "1356921330604666888",
        "createTime": "2022-08-09 10:59:39.868",
        "updateBy": "1356921330604666888",
        "updateTime": "2022-08-09 10:59:39.868",
        "operateConfigList": [
            {
                "id": "220809105940012776",
                "mainListId": "220809105940012761",
                "operateCode": "create",
                "operateName": "新增",
                "operateAlias": null,
                "operateIcon": "plus-square",
                "permissionKey": "create",
                "positionArea": "LEFT_TOOLBAR",
                "displayType": "BUTTON",
                "showCondition": null,
                "actionCondition": null,
                "sortIndex": 1000,
                "enabled": 0,
                "remark": null
            },
            {
                "id": "220809105940012777",
                "mainListId": "220809105940012761",
                "operateCode": "update",
                "operateName": "修改",
                "operateAlias": null,
                "operateIcon": "form",
                "permissionKey": "update",
                "positionArea": "OPERATE_COLUMN",
                "displayType": "ICON",
                "showCondition": null,
                "actionCondition": null,
                "sortIndex": 1000,
                "enabled": 0,
                "remark": null
            },
            {
                "id": "220809105940012778",
                "mainListId": "220809105940012761",
                "operateCode": "approval",
                "operateName": "办理",
                "operateAlias": null,
                "operateIcon": "highlight",
                "permissionKey": "approval",
                "positionArea": "OPERATE_COLUMN",
                "displayType": "ICON",
                "showCondition": null,
                "actionCondition": null,
                "sortIndex": 1000,
                "enabled": 0,
                "remark": null
            },
            {
                "id": "220809105940012779",
                "mainListId": "220809105940012761",
                "operateCode": "delete",
                "operateName": "删除",
                "operateAlias": null,
                "operateIcon": "delete",
                "permissionKey": "delete",
                "positionArea": "OPERATE_COLUMN",
                "displayType": "ICON",
                "showCondition": null,
                "actionCondition": null,
                "sortIndex": 1000,
                "enabled": 0,
                "remark": null
            },
            {
                "id": "220809105940012780",
                "mainListId": "220809105940012761",
                "operateCode": "imports",
                "operateName": "导入",
                "operateAlias": null,
                "operateIcon": "cloud-upload",
                "permissionKey": "imports",
                "positionArea": "RIGHT_TOOLBAR",
                "displayType": "ICON",
                "showCondition": null,
                "actionCondition": null,
                "sortIndex": 1000,
                "enabled": 0,
                "remark": null
            },
            {
                "id": "220809105940012781",
                "mainListId": "220809105940012761",
                "operateCode": "exports",
                "operateName": "导出",
                "operateAlias": null,
                "operateIcon": "cloud-download",
                "permissionKey": "exports",
                "positionArea": "RIGHT_TOOLBAR",
                "displayType": "ICON",
                "showCondition": null,
                "actionCondition": null,
                "sortIndex": 1000,
                "enabled": 0,
                "remark": null
            },
            {
                "id": "220809105940012782",
                "mainListId": "220809105940012761",
                "operateCode": "tableReload",
                "operateName": "表格刷新",
                "operateAlias": null,
                "operateIcon": "reload",
                "permissionKey": "read",
                "positionArea": "RIGHT_TOOLBAR",
                "displayType": "ICON",
                "showCondition": null,
                "actionCondition": null,
                "sortIndex": 1000,
                "enabled": 0,
                "remark": null
            },
            {
                "id": "220809105940012783",
                "mainListId": "220809105940012761",
                "operateCode": "tableSetting",
                "operateName": "表格列设置",
                "operateAlias": null,
                "operateIcon": "setting",
                "permissionKey": "read",
                "positionArea": "RIGHT_TOOLBAR",
                "displayType": "ICON",
                "showCondition": null,
                "actionCondition": null,
                "sortIndex": 1000,
                "enabled": 0,
                "remark": null
            },
            {
                "id": "220809105940012784",
                "mainListId": "220809105940012761",
                "operateCode": "search",
                "operateName": "查询",
                "operateAlias": null,
                "operateIcon": "search",
                "permissionKey": "read",
                "positionArea": "SEARCH_AREA",
                "displayType": "BUTTON",
                "showCondition": null,
                "actionCondition": null,
                "sortIndex": 1000,
                "enabled": 0,
                "remark": null
            },
            {
                "id": "220809105940012785",
                "mainListId": "220809105940012761",
                "operateCode": "reset",
                "operateName": "重置",
                "operateAlias": null,
                "operateIcon": "reload",
                "permissionKey": "read",
                "positionArea": "SEARCH_AREA",
                "displayType": "BUTTON",
                "showCondition": null,
                "actionCondition": null,
                "sortIndex": 1000,
                "enabled": 0,
                "remark": null
            }
        ],
        "quotaDefineList": [
            {
                "id": "220809105940012762",
                "mainListId": "220809105940012761",
                "quotaId": null,
                "quotaName": "主键",
                "quotaCode": "id",
                "dataValueType": "主键型",
                "displayType": null,
                "displayAttrs": {required: true},
                "relatedType": null,
                "relatedObject": null,
                "relatedMode": null,
                "displayState": null,
                "columnWidth": null,
                "dataConvert": null,
                "mergeEnabled": null,
                "sortAttrs": null,
                "alignAttrs": null,
                "styleAttrs": null,
                "searchAttrs": {attr: "id"},
                "filterAttrs": null,
                "linkAttrs": null,
                "sortIndex": 1000
            },
            {
                "id": "220809105940012763",
                "mainListId": "220809105940012761",
                "quotaId": null,
                "quotaName": "主表ID",
                "quotaCode": "mainId",
                "dataValueType": "字符型",
                "displayType": null,
                "displayAttrs": {required: true},
                "relatedType": null,
                "relatedObject": null,
                "relatedMode": null,
                "displayState": null,
                "columnWidth": null,
                "dataConvert": null,
                "mergeEnabled": null,
                "sortAttrs": null,
                "alignAttrs": null,
                "styleAttrs": null,
                "searchAttrs": null,
                "filterAttrs": null,
                "linkAttrs": null,
                "sortIndex": 1000
            },
            {
                "id": "220809105940012764",
                "mainListId": "220809105940012761",
                "quotaId": null,
                "quotaName": "数据类型, 如项目编号",
                "quotaCode": "dataCode",
                "dataValueType": "字符型",
                "displayType": null,
                "displayAttrs": {required: true},
                "relatedType": null,
                "relatedObject": null,
                "relatedMode": null,
                "displayState": null,
                "columnWidth": null,
                "dataConvert": null,
                "mergeEnabled": null,
                "sortAttrs": null,
                "alignAttrs": null,
                "styleAttrs": null,
                "searchAttrs": null,
                "filterAttrs": null,
                "linkAttrs": null,
                "sortIndex": 1000
            },
            {
                "id": "220809105940012765",
                "mainListId": "220809105940012761",
                "quotaId": null,
                "quotaName": "业务编号",
                "quotaCode": "busCode",
                "dataValueType": "字符型",
                "displayType": null,
                "displayAttrs": {required: true},
                "relatedType": null,
                "relatedObject": null,
                "relatedMode": null,
                "displayState": null,
                "columnWidth": null,
                "dataConvert": null,
                "mergeEnabled": null,
                "sortAttrs": null,
                "alignAttrs": null,
                "styleAttrs": null,
                "searchAttrs": null,
                "filterAttrs": null,
                "linkAttrs": null,
                "sortIndex": 1000
            },
            {
                "id": "220809105940012766",
                "mainListId": "220809105940012761",
                "quotaId": null,
                "quotaName": "功能编号",
                "quotaCode": "funcCode",
                "dataValueType": "字符型",
                "displayType": null,
                "displayAttrs": {required: true},
                "relatedType": null,
                "relatedObject": null,
                "relatedMode": null,
                "displayState": null,
                "columnWidth": null,
                "dataConvert": null,
                "mergeEnabled": null,
                "sortAttrs": null,
                "alignAttrs": null,
                "styleAttrs": null,
                "searchAttrs": null,
                "filterAttrs": null,
                "linkAttrs": null,
                "sortIndex": 1000
            },
            {
                "id": "220809105940012767",
                "mainListId": "220809105940012761",
                "quotaId": null,
                "quotaName": "环节编号",
                "quotaCode": "stageCode",
                "dataValueType": "字符型",
                "displayType": null,
                "displayAttrs": {required: true},
                "relatedType": null,
                "relatedObject": null,
                "relatedMode": null,
                "displayState": null,
                "columnWidth": null,
                "dataConvert": null,
                "mergeEnabled": null,
                "sortAttrs": null,
                "alignAttrs": null,
                "styleAttrs": null,
                "searchAttrs": null,
                "filterAttrs": null,
                "linkAttrs": null,
                "sortIndex": 1000
            },
            {
                "id": "220809105940012768",
                "mainListId": "220809105940012761",
                "quotaId": null,
                "quotaName": "环节状态: 2.进行中|3.已结束",
                "quotaCode": "stageState",
                "dataValueType": "数字型",
                "displayType": null,
                "displayAttrs": {required: true},
                "relatedType": null,
                "relatedObject": null,
                "relatedMode": null,
                "displayState": null,
                "columnWidth": null,
                "dataConvert": null,
                "mergeEnabled": null,
                "sortAttrs": null,
                "alignAttrs": null,
                "styleAttrs": null,
                "searchAttrs": {attr: "stageState"},
                "filterAttrs": null,
                "linkAttrs": null,
                "sortIndex": 1000
            },
            {
                "id": "220809105940012769",
                "mainListId": "220809105940012761",
                "quotaId": null,
                "quotaName": "是否生效",
                "quotaCode": "effectiveEnabled",
                "dataValueType": "数字型",
                "displayType": null,
                "displayAttrs": {required: true},
                "relatedType": null,
                "relatedObject": null,
                "relatedMode": null,
                "displayState": null,
                "columnWidth": null,
                "dataConvert": null,
                "mergeEnabled": null,
                "sortAttrs": null,
                "alignAttrs": null,
                "styleAttrs": null,
                "searchAttrs": {attr: "effectiveEnabled"},
                "filterAttrs": null,
                "linkAttrs": null,
                "sortIndex": 1000
            },
            {
                "id": "220809105940012770",
                "mainListId": "220809105940012761",
                "quotaId": null,
                "quotaName": "其他生效条件",
                "quotaCode": "effectiveCondition",
                "dataValueType": "字符型",
                "displayType": null,
                "displayAttrs": {required: true},
                "relatedType": null,
                "relatedObject": null,
                "relatedMode": null,
                "displayState": null,
                "columnWidth": null,
                "dataConvert": null,
                "mergeEnabled": null,
                "sortAttrs": null,
                "alignAttrs": null,
                "styleAttrs": null,
                "searchAttrs": null,
                "filterAttrs": null,
                "linkAttrs": null,
                "sortIndex": 1000
            },
            {
                "id": "220809105940012771",
                "mainListId": "220809105940012761",
                "quotaId": null,
                "quotaName": "描述",
                "quotaCode": "remark",
                "dataValueType": "字符型",
                "displayType": null,
                "displayAttrs": {required: true},
                "relatedType": null,
                "relatedObject": null,
                "relatedMode": null,
                "displayState": null,
                "columnWidth": null,
                "dataConvert": null,
                "mergeEnabled": null,
                "sortAttrs": null,
                "alignAttrs": null,
                "styleAttrs": null,
                "searchAttrs": {attr: "remark"},
                "filterAttrs": null,
                "linkAttrs": null,
                "sortIndex": 1000
            },
            {
                "id": "220809105940012772",
                "mainListId": "220809105940012761",
                "quotaId": null,
                "quotaName": "创建人",
                "quotaCode": "createBy",
                "dataValueType": "字符型",
                "displayType": null,
                "displayAttrs": {required: true},
                "relatedType": null,
                "relatedObject": null,
                "relatedMode": null,
                "displayState": null,
                "columnWidth": null,
                "dataConvert": null,
                "mergeEnabled": null,
                "sortAttrs": null,
                "alignAttrs": null,
                "styleAttrs": null,
                "searchAttrs": {attr: "createBy"},
                "filterAttrs": null,
                "linkAttrs": null,
                "sortIndex": 1000
            },
            {
                "id": "220809105940012773",
                "mainListId": "220809105940012761",
                "quotaId": null,
                "quotaName": "修改人",
                "quotaCode": "updateBy",
                "dataValueType": "字符型",
                "displayType": null,
                "displayAttrs": {required: true},
                "relatedType": null,
                "relatedObject": null,
                "relatedMode": null,
                "displayState": null,
                "columnWidth": null,
                "dataConvert": null,
                "mergeEnabled": null,
                "sortAttrs": null,
                "alignAttrs": null,
                "styleAttrs": null,
                "searchAttrs": {attr: "updateBy"},
                "filterAttrs": null,
                "linkAttrs": null,
                "sortIndex": 1000
            },
            {
                "id": "220809105940012774",
                "mainListId": "220809105940012761",
                "quotaId": null,
                "quotaName": "创建时间",
                "quotaCode": "createTime",
                "dataValueType": "日期型",
                "displayType": null,
                "displayAttrs": {required: true},
                "relatedType": null,
                "relatedObject": null,
                "relatedMode": null,
                "displayState": null,
                "columnWidth": null,
                "dataConvert": null,
                "mergeEnabled": null,
                "sortAttrs": null,
                "alignAttrs": null,
                "styleAttrs": null,
                "searchAttrs": null,
                "filterAttrs": null,
                "linkAttrs": null,
                "sortIndex": 1000
            },
            {
                "id": "220809105940012775",
                "mainListId": "220809105940012761",
                "quotaId": null,
                "quotaName": "修改时间",
                "quotaCode": "updateTime",
                "dataValueType": "日期型",
                "displayType": null,
                "displayAttrs": {required: true},
                "relatedType": null,
                "relatedObject": null,
                "relatedMode": null,
                "displayState": null,
                "columnWidth": null,
                "dataConvert": null,
                "mergeEnabled": null,
                "sortAttrs": null,
                "alignAttrs": null,
                "styleAttrs": null,
                "searchAttrs": null,
                "filterAttrs": null,
                "linkAttrs": null,
                "sortIndex": 1000
            }
        ]
    }
    let renderDOM = new Object
    renderDOM.page = 'page'                             // 暂时写死page 后期动态更换 type: 'page|component' 
    renderDOM.datas = renderData(data)                   // 定义页面状态
    renderDOM.dataSources = renderDataSourcce(data)     // 定义数据源
    renderDOM.templates = renderTemplate(data)           // 定义数据模板
    renderDOM.methods = renderMethods(data)
    return renderDOM
}